package com.cip.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.velocity.app.VelocityEngine;

import com.cip.admin.dao.InstituteDAO;
import com.cip.admin.model.Institute;
import com.cip.admin.service.InstituteService;
import com.cip.common.Constants;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;

public class InstituteServiceImpl implements InstituteService{

	private InstituteDAO instituteDAO;
	private VelocityEngine velocityEngine;
 

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
	
	public List<Institute> getAllInsList() throws Exception {
		return instituteDAO.getAllInsList();
	}

	public Institute getInstituteById(int id) throws Exception {
		return instituteDAO.getInstituteById(id);
	}

	public Institute saveOrUpdateInstitute(Institute institute) throws Exception {
		return instituteDAO.saveOrUpdateInstitute(institute);
	}

	public Institute editInstitute(Institute institute) throws Exception {
		return instituteDAO.editInstitute(institute);
	}	

	public InstituteDAO getInstituteDAO() {
		return instituteDAO;
	}

	public void setInstituteDAO(InstituteDAO instituteDAO) {
		this.instituteDAO = instituteDAO;
	}

	
	public String getInstGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Institute> instList = getAllInsList();
			
			List<Map<String,Object>> instArrayList = new ArrayList<Map<String,Object>>();
			
			System.out.println("instList============="+instList);
			
			for (Institute institute : instList) {
				Map<String,Object> instObj = new HashMap<String, Object>();
				instObj.put("instituteName", institute.getInstituteName());
				instObj.put("phoneNumber", institute.getPhoneNumber());
				instObj.put("emailId", institute.getEmailId());
				instObj.put("action", "<a href='/editInstitute?method=showEditInstitute&institute.id="+institute.getId()+"' style='text-decoration:underline;color:blue;'>View</a>");
				
				System.out.println("instArray================"+instObj);
				
				instArrayList.add(instObj);			
			}
			
			System.out.println("instArrayList=========="+instArrayList);
			
			gridSource = generateScriptForProcess(velocityEngine, instArrayList, locale);
			
			System.out.println("gridSource===================="+gridSource);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> instArrayList, Locale locale) throws Exception {
		
		System.out.println("instArrayList========="+instArrayList);
		System.out.println("locale========="+locale);

		return GridUtil.generateScriptForProcess(velocityEngine, instArrayList, createMyInstuteGridColumnMapping(locale));
		
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createMyInstuteGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Institute List");
		context.put("gridId", "MY_INSTUTE");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Name','Contact No','Email','Action']";
        CommonUtil.createFieldNameList(fieldNameList, "instituteName", "200", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "phoneNumber", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "emailId", "100","left", "", "false");
        CommonUtil.createFieldNameList(fieldNameList, "action", "70", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		System.out.println("context====================="+context);
		
		return context;
	}
	
	/**
	 * Add needed column and it mapping for process list
	 * 
	 * @param context
	 */
	/*private static void createProcessListGridColumnMapping(Map<String, Object> context,Locale locale) {
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		context.put("title", appResourceBundle.getString("manage.listProcess"));
		context.put("gridId", "LIST_PROCESS");
		context.put("needCheckbox", true);
		context.put("needTreeStructure", true);
		context.put("dynamicGridWidth", "organizationGridWidth");
		context.put("dynamicGridHeight", "organizationGridHeight");
		String columnNames = "['Id','Process Name','Description','Created At','Version No','Process Version','Deploy','Process Key','State','Action','Delete','Export','Resource Name','DeploymentId','View/Export Preview','Browse','Classification']";
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "","true");
//		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left",
//				"_getProcessInfo", "false");
		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left","_editProcess", "false");	
		CommonUtil.createFieldNameList(fieldNameList, "description", "125", "left","", "false");
		CommonUtil.createFieldNameList(fieldNameList, "createdAt", "75","center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "version", "50", "center","", "false");
		CommonUtil.createFieldNameList(fieldNameList, "showVersion", "75", "center","_showProcessVersions", "false");
		CommonUtil.createFieldNameList(fieldNameList, "deploy", "50", "center","_deployProcess", "true");
		CommonUtil.createFieldNameList(fieldNameList, "key", "50", "center","", "true");
		CommonUtil.createFieldNameList(fieldNameList, "suspensionState", "50", "center","", "true");
		CommonUtil.createFieldNameList(fieldNameList, "suspend", "40", "center","_suspensionProcess", "true");
		CommonUtil.createFieldNameList(fieldNameList, "delete", "40", "center","_deleteAllProcessVersions", "false");
		CommonUtil.createFieldNameList(fieldNameList, "export", "40", "center","_downloadProcessXml", "false");
		CommonUtil.createFieldNameList(fieldNameList, "resourceName", "50", "center","", "true");
		CommonUtil.createFieldNameList(fieldNameList, "deploymentId", "50", "center","", "true");
		CommonUtil.createFieldNameList(fieldNameList, "view", "100", "center","_xmlViewLink", "false");
		CommonUtil.createFieldNameList(fieldNameList, "browse", "50", "center","_xmlBrowseLink", "true");
		CommonUtil.createFieldNameList(fieldNameList, "classificationId", "50", "center","", "true");
		context.put("noOfRecords", "16");
		context.put("columnNames", columnNames);
		context.put("fieldNameList", fieldNameList);
	}*/
}
